#!/bin/bash

# if error, exit immediately
set -e

# config params
# builder work dir
WORK_DIR=$(cd "$(dirname "$0")" && pwd -P)
echo "builder work dir: ${WORK_DIR}"
# target directory
TARGET_DIR=$(dirname "$(pwd)")
echo "builder target dir: ${TARGET_DIR}"
# RootFS directory:Compile environment
ROOTFS_DIR="${TARGET_DIR}/sys_rootfs"
# pool directory:system packages
POOL_DIR="${TARGET_DIR}/pool"
# CPU architecture:ARCH="amd64"、ARCH="arm64"
ARCH="amd64"
# SysRootFS version
SYS_ROOTFS_VERSION="1.0.0-${ARCH}"
# SysRootFS name
SYS_ROOTFS_NAMR="SysRootFS-${SYS_ROOTFS_VERSION}.tar.gz"

# clean pool dir
if [ ! -d ${POOL_DIR} ]; then
    mkdir -p ${POOL_DIR}
fi

# publish SysRootFS
# clean old SysRootFS
if [ -f ${POOL_DIR}/${SYS_ROOTFS_NAMR} ]; then
    echo "Remove old ${SYS_ROOTFS_NAMR} ..."
    rm -f ${POOL_DIR}/${SYS_ROOTFS_NAMR}   
fi
# package SysRootFS edisk
echo "Package ${SYS_ROOTFS_NAMR} ..."
cd ${ROOTFS_DIR}
tar -czf ${POOL_DIR}/${SYS_ROOTFS_NAMR} --exclude=boot .
echo "Package ${SYS_ROOTFS_NAMR} is created at ${POOL_DIR}/${SYS_ROOTFS_NAMR}"

echo "Finish Build SysRootFS..."
